/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss.component.dynproperty.support.hibernate;

import com.jinhe.tss.component.dynproperty.support.AbstractDynEntity;
import com.jinhe.tss.component.dynproperty.support.DynProperty;
import com.jinhe.tss.core.exception.BusinessException;
import com.jinhe.tss.core.persistence.BaseDao;
import com.jinhe.tss.core.persistence.IEntity;

/** 
 * <p> AbstractHibernateDao.java </p> 
 * 
 * @author Jon.King 2008/04/14 10:18:47 $ 
 */
public abstract class DynAbstractHibernateDao extends BaseDao<IEntity> implements IDynAbstractHibernateDao{
    
    public DynAbstractHibernateDao(Class<IEntity> type) {
        super(type);
    }

    /**
     * **********************************************************************************************
     * ***************************     以下为动态属性值存取删等各种操作封装     ****************************
     * **********************************************************************************************
     */
    
    private DynProperty dp = new HibernateDynProperty();
 
    /**
     * 保存某个对象和对象的所有动态属性值
     * @param entity
     * @return
     */
    public AbstractDynEntity saveDynEntity(AbstractDynEntity entity) {
        dp.init( this );
        
        create(entity); //保存动态实体自身
        
        dp.saveDynProperti4Entity(entity);
      
        return entity;
    }

    /**
     * 获取某个对象和对象的所有动态属性值
     * @param clazz
     * @param id
     * @return
     */
    public AbstractDynEntity getDynEntity(Class<?> clazz, Long id) {
        dp.init( this );
        
        Object obj = getEntity(clazz, id);
        if(!(obj instanceof AbstractDynEntity)) {
            throw new BusinessException("获取到的对象非AbstractDynEntity子类，不能调用本方法：getDynEntity(Long id, Class clazz)。");
        }
         
        return dp.getDynProperti4Entity((AbstractDynEntity) obj);
    }
 
    /**
     * 删除某个对象和对象的所有动态属性值
     * @param obj
     */
    public void removeDynEntity(Object obj) {
        if(!(obj instanceof AbstractDynEntity))
            throw new BusinessException("对象非AbstractDynEntity子类，不能调用本方法：removeDynEntity(Object obj)。");
        
        dp.init( this );
        
        AbstractDynEntity entity = (AbstractDynEntity) obj;
        delete(entity);
               
        dp.removeDynProperti4Entity(entity);
    }
}

